package com.tuanzi.loan.business.repository.xhh.settlementsystem.impl;

import com.tuanzi.loan.business.repository.xhh.settlementsystem.custom.XhhSettlementRemainRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementRemainRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementRemainResponse;
import org.apache.commons.lang3.time.DateUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.XhhLoanSettlementRemain.XHH_LOAN_SETTLEMENT_REMAIN;

/**
 * @author caiwanlin
 * @create 2018-08-22 17:23
 */
public class XhhSettlementRemainRepositoryImpl extends JOOQBaseRepositoryPlus implements XhhSettlementRemainRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return XHH_LOAN_SETTLEMENT_REMAIN;
    }


    @Override
    public Page<XhhSettlementRemainResponse> list(XhhSettlementRemainRequest searchRequest, Pageable pageable) {
        Long companyId = searchRequest.getCompanyId();
        Integer remainType = searchRequest.getRemainType();
        Date startTime = searchRequest.getStartTime();
        Date endTime = searchRequest.getEndTime();
        if (endTime != null){
            endTime = DateUtils.addHours(endTime, 23);
            endTime = DateUtils.addMinutes(endTime,59);
            endTime = DateUtils.addSeconds(endTime,59);
        }
        fields(XHH_LOAN_SETTLEMENT_REMAIN.fields());
        eq(XHH_LOAN_SETTLEMENT_REMAIN.COMPANY_ID,companyId);
        eq(XHH_LOAN_SETTLEMENT_REMAIN.REMAIN_TYPE,remainType);
        between(XHH_LOAN_SETTLEMENT_REMAIN.CREATE_TIME,startTime,endTime);
        orderBy(XHH_LOAN_SETTLEMENT_REMAIN.CREATE_TIME, Sort.Direction.DESC);

        Page<XhhSettlementRemainResponse> search = search(pageable, record -> {
            XhhSettlementRemainResponse response = new XhhSettlementRemainResponse();
            response.setId(record.get(XHH_LOAN_SETTLEMENT_REMAIN.ID));
            response.setCompanyId(record.get(XHH_LOAN_SETTLEMENT_REMAIN.COMPANY_ID));
            response.setCompanyName(record.get(XHH_LOAN_SETTLEMENT_REMAIN.COMPANY_NAME));
            response.setRemainType(record.get(XHH_LOAN_SETTLEMENT_REMAIN.REMAIN_TYPE));
            response.setRelateId(record.get(XHH_LOAN_SETTLEMENT_REMAIN.RELATE_ID));
            response.setPrePrepayAmount(record.get(XHH_LOAN_SETTLEMENT_REMAIN.PRE_PREPAY_AMOUNT));
            response.setPrepayAmount(record.get(XHH_LOAN_SETTLEMENT_REMAIN.PREPAY_AMOUNT));
            response.setPreExpendAmount(record.get(XHH_LOAN_SETTLEMENT_REMAIN.PRE_EXPEND_AMOUNT));
            response.setExpendAmount(record.get(XHH_LOAN_SETTLEMENT_REMAIN.EXPEND_AMOUNT));
            response.setPreRemainAmount(record.get(XHH_LOAN_SETTLEMENT_REMAIN.PRE_REMAIN_AMOUNT));
            response.setRemainAmount(record.get(XHH_LOAN_SETTLEMENT_REMAIN.REMAIN_AMOUNT));
            response.setCreator(record.get(XHH_LOAN_SETTLEMENT_REMAIN.CREATOR));
            response.setCreateTime(record.get(XHH_LOAN_SETTLEMENT_REMAIN.CREATE_TIME));
            response.setUpdator(record.get(XHH_LOAN_SETTLEMENT_REMAIN.UPDATOR));
            response.setUpdateTime(record.get(XHH_LOAN_SETTLEMENT_REMAIN.UPDATE_TIME));
            return response;
        });

        return search;
    }
}
